<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/1
 * Time: 上午9:09
 */
class Login extends MY_Controller
{
    /**
     * 登录
     * @return [type] [description]
     */
    public function index(){
        $has_error = 0;
        
        if($_POST){
            $post_data = $this->input->post();
            $result['data'] = array();
//            print_r($post_data);
            if(empty($post_data['phone'])){
                $result['code'] = 10001;
                // $result['msg'] = '缺少手机号码';
                echo json_encode($result);exit;
            }
            if(empty($post_data['pwd'])){
                // $result['msg'] = '缺少密码';
                $result['code'] = 10002;
                echo json_encode($result);exit;
            }

            //判断手机号是否存在
            $this->load->model('User_model');
            $user = $this->User_model->get_user_by_phone($post_data['phone']);

            if(empty($user)){
                $result['code'] = 10003;
                // $result['msg'] = '手机号尚未注册';
            }else if($user['is_teacher']==1){
                $result['code'] = 10003;
                // $result['msg'] = '手机号尚未注册';
            }else{
                $password = md5(md5($post_data['pwd']) . $user['rand_str']);
                if($user['password'] != $password){
                    $result['code'] = 10004;
                    // $result['msg'] = '密码错误';
                }else{
                    $result['code'] = 200;
                    $result['data']['uid'] = $user['id'];
                    $result['data']['nick'] = $user['nick'];
                    $result['data']['head_image_url'] = $user['head_image_url'];
                    // $result['msg'] = '登录成功';
                }
            }

            echo json_encode($result);exit;
        }else{
            $result['code'] = 9;
            $result['msg'] = '非法操作';
            echo json_encode($result);exit;
        }
    }
}
